﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYElectricCommon;

namespace YArchitech.Revit.MEP.Calculator
{
	public partial class DlgSelIllAndPower : System.Windows.Forms.Form
	{
		public DlgSelIllAndPower()
		{
			string dbFile = BuildingPlaceCommon.GetDbFile();
			SqLiteHelper.SqlLiteDatabase("Data Source=" + dbFile + ";Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10");
			this.InitializeComponent();
			this.InitTreeControl();
		}

		public DlgSelIllAndPower(string strPlace)
		{
			string dbFile = BuildingPlaceCommon.GetDbFile();
			SqLiteHelper.SqlLiteDatabase("Data Source=" + dbFile + ";Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10");
			this.InitializeComponent();
			this.InitTreeControl();
			foreach (object obj in this.tvDatabase.Nodes)
			{
				TreeNode tnParent = (TreeNode)obj;
				TreeNode treeNode = this.FindNode(tnParent, strPlace);
				if (treeNode != null)
				{
					treeNode.Expand();
					this.tvDatabase.SelectedNode = treeNode;
					this.ShowSelectNode(treeNode);
					break;
				}
			}
		}

		private void tvDatabase_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			TreeNode selectedNode = this.tvDatabase.SelectedNode;
		}

		private void tvDatabase_MouseClick(object sender, MouseEventArgs e)
		{
			TreeNode nodeAt = (sender as TreeView).GetNodeAt(e.X, e.Y);
			this.ShowSelectNode(nodeAt);
		}

		private void InitTreeControl()
		{
			System.Data.DataTable dataTable = Database.ExecQuery("select [Industry] from [IllStandard]");
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					bool flag = false;
					for (int j = 0; j < this.tvDatabase.Nodes.Count; j++)
					{
						if (this.tvDatabase.Nodes[j].Text == dataTable.Rows[i][0].ToString())
						{
							flag = true;
						}
					}
					if (!flag)
					{
						this.tvDatabase.Nodes.Add(dataTable.Rows[i][0].ToString());
					}
				}
			}
			foreach (object obj in this.tvDatabase.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				string text = treeNode.Text;
				dataTable = Database.ExecQuery("select [BuildingType] from [IllStandard] where [Industry] ='" + text + "'");
				if (dataTable != null)
				{
					foreach (object obj2 in dataTable.Rows)
					{
						DataRow dataRow = (DataRow)obj2;
						bool flag2 = false;
						IEnumerator enumerator3 = treeNode.Nodes.GetEnumerator();
						{
							while (enumerator3.MoveNext())
							{
								if (((TreeNode)enumerator3.Current).Text == dataRow[0].ToString())
								{
									flag2 = true;
								}
							}
						}
						if (!flag2)
						{
							treeNode.Nodes.Add(dataRow[0].ToString());
						}
					}
				}
			}
			foreach (object obj3 in this.tvDatabase.Nodes)
			{
				foreach (object obj4 in ((TreeNode)obj3).Nodes)
				{
					TreeNode treeNode2 = (TreeNode)obj4;
					string text2 = treeNode2.Text;
					dataTable = Database.ExecQuery("select [Place] from [IllStandard] where [BuildingType] ='" + text2 + "'");
					if (dataTable != null)
					{
						foreach (object obj5 in dataTable.Rows)
						{
							DataRow dataRow2 = (DataRow)obj5;
							bool flag3 = false;
							IEnumerator enumerator4 = treeNode2.Nodes.GetEnumerator();
							{
								while (enumerator4.MoveNext())
								{
									if (((TreeNode)enumerator4.Current).Text == dataRow2[0].ToString())
									{
										flag3 = true;
									}
								}
							}
							if (!flag3)
							{
								treeNode2.Nodes.Add(dataRow2[0].ToString());
							}
						}
					}
				}
			}
		}

		private SelIllAndPowerData GetSelNodeData(TreeNode node)
		{
			SelIllAndPowerData selIllAndPowerData = new SelIllAndPowerData();
			TreeNode parent = node.Parent;
			if (parent != null)
			{
				TreeNode parent2 = parent.Parent;
				if (parent2 != null)
				{
					string text = parent2.Text;
					string text2 = parent.Text;
					string text3 = node.Text;
					System.Data.DataTable dataTable = Database.ExecQuery("select [RefPlane], [NowPD], [TargetPD], [IllStandard], [GlareValue], [Ra] from [IllStandard] where [Place] ='" + text3 + "'");
					if (dataTable != null)
					{
						selIllAndPowerData.Industry = text;
						selIllAndPowerData.BuildingType = text2;
						selIllAndPowerData.Place = text3;
						selIllAndPowerData.RefPlane = dataTable.Rows[0][0].ToString();
						selIllAndPowerData.NowPD = Convert.ToDouble(dataTable.Rows[0][1].ToString());
						selIllAndPowerData.TargetPD = Convert.ToDouble(dataTable.Rows[0][2].ToString());
						selIllAndPowerData.IllStandard = Convert.ToDouble(dataTable.Rows[0][3].ToString());
						selIllAndPowerData.GlareValue = Convert.ToDouble(dataTable.Rows[0][4].ToString());
						selIllAndPowerData.Ra = Convert.ToDouble(dataTable.Rows[0][5].ToString());
					}
				}
			}
			return selIllAndPowerData;
		}

		private void ShowSelectNode(TreeNode node)
		{
			this.m_IllAndPowerData = this.GetSelNodeData(node);
			this.lblReferencePalace.Text = this.m_IllAndPowerData.RefPlane;
			this.lblPowerDensityObjV.Text = this.m_IllAndPowerData.TargetPD.ToString();
			this.lblPowerDensityNowV.Text = this.m_IllAndPowerData.NowPD.ToString();
			this.lblGlareValue.Text = this.m_IllAndPowerData.GlareValue.ToString();
			this.lblRa.Text = this.m_IllAndPowerData.Ra.ToString();
			this.lblIlluminateStandard.Text = this.m_IllAndPowerData.IllStandard.ToString();
			this.lblPowerDensityNowV.ForeColor = System.Drawing.Color.Magenta;
			this.lblIlluminateStandard.ForeColor = System.Drawing.Color.Magenta;
		}

		private TreeNode FindNode(TreeNode tnParent, string strValue)
		{
			if (tnParent == null)
			{
				return null;
			}
			if (tnParent.Text == strValue)
			{
				return tnParent;
			}
			if (tnParent.Nodes.Count == 0)
			{
				return null;
			}
			TreeNode treeNode = tnParent;
			TreeNode treeNode2 = treeNode.FirstNode;
			IL_8E:
			while (treeNode2 != null && treeNode2 != tnParent)
			{
            IL_76:
				while (treeNode2 != null)
				{
					if (treeNode2.Text == strValue)
					{
						return treeNode2;
					}
					if (treeNode2.Nodes.Count > 0)
					{
						treeNode = treeNode2;
						treeNode2 = treeNode2.FirstNode;
					}
					else if (treeNode2 != treeNode.LastNode)
					{
						treeNode2 = treeNode2.NextNode;
					}
					else
					{
						
						while (treeNode2 != tnParent && treeNode2 == treeNode.LastNode)
						{
							treeNode2 = treeNode;
							treeNode = treeNode.Parent;
						}
						if (treeNode2 != tnParent)
						{
							treeNode2 = treeNode2.NextNode;
							goto IL_8E;
						}
						goto IL_8E;
					}
				}
				goto IL_76;
			}
			return null;
		}

		private void DlgSelIllAndPower_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		public SelIllAndPowerData m_IllAndPowerData;
	}
}
